import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Snake here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */

public class Snake extends Trap
{
    private int yVel=0;
    private int skinIndex=0;
    private boolean isAlive=true;
    private boolean jumpingSnake=false;
    private int dir;
    private int xVel;
    private CollisionBox colDown=new CollisionBox(this,0,30,10,10);
    private CollisionBox colSnake=new CollisionBox(this,0,0,90,90);
    private boolean addedToWorld=false;
    private boolean exists=true;
    private GreenfootImage[][] skins=new GreenfootImage[3][2];
    
    private static final int RIGHT=0;
    private static final int LEFT=1;
    
    public Snake(int x,int del,int iniD)
    {
        super(x,del,iniD);
        
        xVel=10;
        damage=10;
        
        int ran=Greenfoot.getRandomNumber(5);
        
        for(int i=0;i<3;i++)
        {
            skins[i][0]=new GreenfootImage("snake_"+ran+"_"+i+".png");
            skins[i][1]=new GreenfootImage("snake_"+ran+"_"+i+".png");
            skins[i][1].mirrorHorizontally();
        }
        
        switch(ran)
        {
            case 0:     xVel=20;
                        break;
            case 1:     xVel=12;
                        damage=20;
                        break;
            case 2:     damage=20;
                        break;
            case 3:     damage=50;
                        xVel=5;
                        break;
            default:    xVel=14;
                        jumpingSnake=true;
                        break;
        }
        
        setImage(skins[0][0]);
    }
        
        
    public void act() 
    {
        timer.tick();
        activeTimer.tick();
        
        GameWorld world=(GameWorld)getWorld();
        
        
        if(addedToWorld==false)
        {
            addedToWorld=true;
            world.addObject(colDown,getX(),getY());
            world.addObject(colSnake,getX(),getY());
        }
        
        
        if(active==true || !activeTimer.wait(initialDelay))     
        activeImage();
        if(active==false)
        return;
        
        if(colDown.isTouchingGround())
        {
            yVel=-1;
            setLocation(getX(),world.getHeight()-120);
            
            if(jumpingSnake && isAlive)
            yVel-=10;
        }
        
        yVel+=1;
        setLocation(world.getScrollx()+360+posX,getY()+yVel);
        
        if(isAlive)
        {
            setLocation(world.getScrollx()+360+posX,getY());
            
            if(isTouchingWhip())
            {
                isAlive=false;
                skinIndex=2;
                setImage(skins[skinIndex][dir]);
                timer.reset();
            }
            
            if(isTouchingPlayer())
            getPlayer().getHurt(damage);
        
            if(timer.wait(100))
            return;
        
            if(getX()<getPlayer().getX())
            {
                posX+=xVel;
                dir=RIGHT;
            }
            else
            {
                posX-=xVel;
                dir=LEFT;
            }
        
            Whip whip=world.getWhip();
            
            if(getY()>world.getHeight() || (isTouching(Trap.class) && !isTouching(Snake.class) && !isTouching(SnakeSpawner.class)))
            {
                isAlive=false;
                skinIndex=2;
                timer.reset();
            } 
            
            if(skinIndex==0 && skinIndex<2)
            skinIndex=1;
            else
            if(skinIndex<2)
            skinIndex=0;
            
            setImage(skins[skinIndex][dir]);
        }else
            disappear();
            
        if(getImage().getTransparency()==0)
        {
            world.removeObject(this);
            world.removeObject(colDown);
            world.removeObject(colSnake);
        }
        
    }  
    
    public void disappear()
    {
        int scrollx=((GameWorld)getWorld()).getScrollx();
        
        GreenfootImage image=getImage();
        
        if(image.getTransparency()>50)
        {
            if(!timer.wait(100))
            image.setTransparency(image.getTransparency()-25);
        }else  
        {
            image.setTransparency(0);
            setLocation(scrollx+posX+getWorld().getWidth()/2,0);
            exists=false;
        }
        
        setImage(image);
    }
    
    public boolean isTouchingWhip()
    {
        if(!intersects(((GameWorld)getWorld()).getWhip()))
        return(false);
        
        return(touch(((GameWorld)getWorld()).getWhip()));
    }
    
    public void kill()
    {
        isAlive=false;
    }
}
